﻿using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Shell;

namespace WindowsStyle
{
    public partial class MainView : Window
    {
        public MainView()
        {
            InitializeComponent();
            min.Click += (sender, e) => this.WindowState = WindowState.Minimized;
            max.Click += (sender, e) => ButtonBase_OnClick(sender, e);
            close.Click += (sender, e) => this.Close();
            this.HeaderBorder.MouseLeftButtonDown += HeaderBorder_MouseLeftButtonDown;

            this.DataContext = new MainViewModel();
        }
        // 后台数据准备
        public ObservableCollection<string> Numbers { get; } = new()
        {
            "1", "2", "4", "5"
        };


        private void HeaderBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.WindowState == WindowState.Maximized)
            {
                // 获取鼠标在屏幕上的坐标
                Point mouseScreenPos = PointToScreen(e.GetPosition(this));

                // 将窗口状态改为正常
                WindowState = WindowState.Normal;

                // 确保布局更新以获取正确尺寸
                UpdateLayout();

                // 计算调整后的新位置
                double screenWidth = SystemParameters.PrimaryScreenWidth;
                double newWidth = ActualWidth;
                double newLeft = mouseScreenPos.X - (mouseScreenPos.X / screenWidth * newWidth);

                // 应用新位置
                Left = newLeft;
                Top = mouseScreenPos.Y - 10; // 可根据需要调整Y坐标

                // 开始拖动
                DragMove();
            }
            else
            {
                DragMove();
            }
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.WindowState == WindowState.Maximized)
            {
                this.WindowState = WindowState.Normal;
            }
            else if (this.WindowState == WindowState.Normal)
            {
                WindowState = WindowState.Maximized;
                //this.MaxHeight=SystemParameters.MaximizedPrimaryScreenHeight;
                //this.MaxWidth=SystemParameters.MaximizedPrimaryScreenWidth;
            }
        }


        #region 隐藏系统原有功能按钮

        [DllImport("user32.dll")]
        private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

        [DllImport("user32.dll")]
        private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

        private const int GWL_STYLE = -16;
        private const int WS_SYSMENU = 0x80000;

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            // 隐藏系统按钮
            IntPtr hwnd = new WindowInteropHelper(this).Handle;
            int style = GetWindowLong(hwnd, GWL_STYLE);
            SetWindowLong(hwnd, GWL_STYLE, style & ~WS_SYSMENU);

        }

        [DllImport("user32.dll")]
        private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

        private const int WM_NCHITTEST = 0x0084;
        private const int HTMAXBUTTON = 9; // 最大化按钮区域标识
        private const int HTCLIENT = 1; // 客户区标识
        #endregion



    }


}